/**
 *Storage工具
 *
 * @class StorageStore
 */
class StorageStore {
  _store: Storage;
  /**
   * Creates an instance of StorageStore.
   * @param {*} store 参数
   * @memberof StorageStore
   */
  constructor(store: Storage) {
    this._store = store;
  }

  /**
   * @function 设置值
   * @param {string} _k 必须参数，属性
   * @param {any} _v 非必须参数，属性值
   * @return {boolean} 返回结果
   */
  setItem<V>(_k: string, _v: V) {
    if (!this._store) return false;
    if (!_v) return false;
    const val = this.filterValue(_v);
    if (!val) return false;
    this._store.setItem(_k, val);
    return true;
  }

  /**
   * @function 获取值
   * @param {string} _k 必须参数，属性
   * @return {string} 返回
   */
  getItem(_k: string) {
    if (!this._store) return;
    let val: any = undefined;
    const kType = this.getType(_k);
    if (kType === "string") {
      const res = this._store.getItem(_k);
      if (!res || res === "undefined" || res === "null") return false;
      const dataobj = JSON.parse(res); //解析出json对象
      if (new Date().getTime() - dataobj.time > 36000000) {
        //如果当前时间-减去存储的元素在创建时候设置的时间 > 过期时间
        // val = "登录已过期";
      } else {
        val = dataobj.val;
      }
    } else {
      val = "key只能为字符串！";
    }
    return val;
  }

  /**
   * @function 移除值
   * @param {string} _k 必须参数，属性
   * @return {void}
   */
  removeItem(_k: string) {
    if (!this._store) return;
    const kType = this.getType(_k);
    if (kType === "string") {
      const res = this._store.removeItem(_k);
      return res;
    }
  }

  /**
   * @function 移除所有
   * @return {void}
   */
  clear() {
    if (!this._store) return;
    this._store.clear();
  }

  /**
   * @function 判断类型
   * @param {any} para 必须参数，判断的值
   * @return {void}
   */
  getType<V>(para: V) {
    const type = typeof para;
    if (type === "number") return "NaN";
    if (type !== "object") return type;
    return Object.prototype.toString
      .call(para)
      .replace(/[\[\]]/g, "") // eslint-disable-line
      .split(" ")[1]
      .toLowerCase();
  }

  /**
   * @function 过滤值
   * @param {any} val 必须参数，过滤的值
   * @return {void}
   */
  filterValue<V>(val: V) {
    const curTime = new Date().getTime(); //获取当前时间
    const vType = this.getType(val);
    const nullVal = ["null", "undefined", "NaN"];
    const stringVal = ["boolen", "number", "string", "object"];
    if (nullVal.indexOf(vType) >= 0)
      return JSON.stringify({ val: "", time: curTime });
    if (stringVal.indexOf(vType) >= 0)
      return JSON.stringify({ val: val, time: curTime });
  }
}

/**
 *LocalStorage
 *
 * @class LocalStorage
 * @extends {StorageStore}
 */
class LocalStorage extends StorageStore {
  constructor(store: Storage) {
    // eslint-disable-line
    super(store);
  }

  // WX_USER_ID = 'WX_USER_ID';
}

/**
 *SessionStorage
 *
 * @class SessionStorage
 * @extends {StorageStore}
 */
class SessionStorage extends StorageStore {
  constructor(store: Storage) {
    // eslint-disable-line
    super(store);
  }

  // WX_SSO_TITLE = 'WX_SSO_TITLE';
}

const lStorage = new LocalStorage(window.localStorage || localStorage);
const sStorage = new SessionStorage(window.sessionStorage || sessionStorage);

export { lStorage, sStorage };
